import pygame
from pygame.sprite import Sprite
# Sprite小精灵，所有元素编组，群体操作


class Bullet(Sprite):

    def __init__(self, screen, setting, ship):
        # 在飞船位置创建一个bullet对象
        super().__init__()
        self.screen = screen
        self.rect = pygame.Rect(0, 0, setting.bullet_width, setting.bullet_height)
        self.rect.centerx = ship.rect.centerx
        self.rect.top = ship.rect.top
        self.y = float(self.rect.y)
        self.color = setting.bullet_color
        self.speed_factor = setting.bullet_speed_factor

    def show_and_update(self):
        self.y -= self.speed_factor
        self.rect.y = self.y
        pygame.draw.rect(self.screen, self.color, self.rect)
